using UnityEngine;

public class BackgroundManager : MonoBehaviour
{
    [Header("背景设置")]
    public Color backgroundColor = new Color(0.1f, 0.1f, 0.2f, 1f); // 深蓝色背景
    public bool useGradient = true;
    public Color topColor = new Color(0.2f, 0.1f, 0.3f, 1f);
    public Color bottomColor = new Color(0.05f, 0.05f, 0.15f, 1f);
    
    [Header("星空效果")]
    public bool enableStars = true;
    public int starCount = 100;
    public float starSize = 0.1f;
    public Color starColor = Color.white;
    
    private Camera mainCamera;
    private GameObject[] stars;
    
    void Start()
    {
        mainCamera = Camera.main;
        SetupBackground();
        
        if (enableStars)
        {
            CreateStars();
        }
        
        Debug.Log("背景环境已设置完成");
    }
    
    void SetupBackground()
    {
        if (mainCamera != null)
        {
            // 设置摄像机背景色
            if (useGradient)
            {
                // 创建渐变背景
                CreateGradientBackground();
            }
            else
            {
                mainCamera.backgroundColor = backgroundColor;
            }
        }
    }
    
    void CreateGradientBackground()
    {
        // 创建一个简单的渐变背景精灵
        GameObject backgroundObj = new GameObject("GradientBackground");
        backgroundObj.transform.parent = transform;
        
        SpriteRenderer spriteRenderer = backgroundObj.AddComponent<SpriteRenderer>();
        
        // 创建渐变纹理
        Texture2D gradientTexture = CreateGradientTexture();
        Sprite gradientSprite = Sprite.Create(gradientTexture, new Rect(0, 0, gradientTexture.width, gradientTexture.height), Vector2.one * 0.5f);
        
        spriteRenderer.sprite = gradientSprite;
        spriteRenderer.sortingOrder = -100; // 确保在最后面
        
        // 调整大小以覆盖整个屏幕
        if (mainCamera != null && mainCamera.orthographic)
        {
            float cameraHeight = mainCamera.orthographicSize * 2;
            float cameraWidth = cameraHeight * mainCamera.aspect;
            
            backgroundObj.transform.localScale = new Vector3(cameraWidth, cameraHeight, 1);
        }
    }
    
    Texture2D CreateGradientTexture()
    {
        int height = 256;
        Texture2D texture = new Texture2D(1, height);
        
        for (int y = 0; y < height; y++)
        {
            float t = (float)y / (height - 1);
            Color color = Color.Lerp(bottomColor, topColor, t);
            texture.SetPixel(0, y, color);
        }
        
        texture.Apply();
        return texture;
    }
    
    void CreateStars()
    {
        stars = new GameObject[starCount];
        
        for (int i = 0; i < starCount; i++)
        {
            GameObject star = CreateStar();
            stars[i] = star;
        }
    }
    
    GameObject CreateStar()
    {
        GameObject star = new GameObject("Star");
        star.transform.parent = transform;
        
        SpriteRenderer spriteRenderer = star.AddComponent<SpriteRenderer>();
        
        // 创建简单的星星纹理
        Texture2D starTexture = CreateStarTexture();
        Sprite starSprite = Sprite.Create(starTexture, new Rect(0, 0, starTexture.width, starTexture.height), Vector2.one * 0.5f);
        
        spriteRenderer.sprite = starSprite;
        spriteRenderer.color = starColor;
        spriteRenderer.sortingOrder = -50;
        
        // 随机位置
        if (mainCamera != null && mainCamera.orthographic)
        {
            float cameraHeight = mainCamera.orthographicSize;
            float cameraWidth = cameraHeight * mainCamera.aspect;
            
            float x = Random.Range(-cameraWidth, cameraWidth);
            float y = Random.Range(-cameraHeight, cameraHeight);
            star.transform.position = new Vector3(x, y, 0);
        }
        
        // 随机大小
        float scale = Random.Range(starSize * 0.5f, starSize * 1.5f);
        star.transform.localScale = Vector3.one * scale;
        
        // 添加闪烁效果
        StarTwinkle twinkle = star.AddComponent<StarTwinkle>();
        twinkle.twinkleSpeed = Random.Range(0.5f, 2f);
        
        return star;
    }
    
    Texture2D CreateStarTexture()
    {
        int size = 8;
        Texture2D texture = new Texture2D(size, size);
        
        // 创建简单的星星形状
        for (int x = 0; x < size; x++)
        {
            for (int y = 0; y < size; y++)
            {
                float distance = Vector2.Distance(new Vector2(x, y), new Vector2(size/2f, size/2f));
                float alpha = Mathf.Clamp01(1f - (distance / (size/2f)));
                texture.SetPixel(x, y, new Color(1, 1, 1, alpha));
            }
        }
        
        texture.Apply();
        return texture;
    }
}

// 星星闪烁效果组件
public class StarTwinkle : MonoBehaviour
{
    public float twinkleSpeed = 1f;
    private SpriteRenderer spriteRenderer;
    private float originalAlpha;
    
    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        originalAlpha = spriteRenderer.color.a;
    }
    
    void Update()
    {
        float alpha = originalAlpha * (0.3f + 0.7f * (Mathf.Sin(Time.time * twinkleSpeed) + 1f) / 2f);
        Color color = spriteRenderer.color;
        color.a = alpha;
        spriteRenderer.color = color;
    }
}